﻿/***********************************************************************
 * <copyright file="InventoryPresenter.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   BangNC
 * Email:    BangNC@buca.vn
 * Website:
 * Create Date: Thursday, March 13, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using WindowsForms.AOnline.Model.BusinessObjects.Dictionary;
using WindowsForms.AOnline.Model.BusinessObjects.Inventory;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.View.Inventory;


namespace WindowsForms.AOnline.Presenter.Inventory.InputInventory
{
    public class InputInventoryPresenter : Presenter<IItemTransactionView> 
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemTransactionPresenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public InputInventoryPresenter(IItemTransactionView view)
            : base(view)
        {
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns></returns>
        public string Save()
        {
            var voucher = new ItemTransactionModel
            {
                ItemTransactionDetails = View.ItemTransactionDetails,
                RefId = View.RefId,
                RefTypeId = View.RefTypeId,
                RefNo = View.RefNo,
                RefDate = View.RefDate,
                CustomerId = View.CustomerId,
                VendorId = View.VendorId,
                Trader = View.Trader,
                StockId = View.StockId,
                TotalAmountOc = View.TotalAmountOc,
                UnitId = View.UnitId,
                TaxCode = View.TaxCode,
                JournalMemo = View.JournalMemo,
                DocumentInclude = View.DocumentInclude,
                TotalAmountTax = View.TotalAmountTax,
                AccountingObjectType = View.AccountingObjectType,
            };
            return View.RefId == 0 ? Model.AddItemTransactionVoucher(voucher) : Model.UpdateItemTransactionVoucher(voucher);
        }

        /// <summary>
        /// Displays the specified reference identifier.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        public void Display(long refId)
        {
            var voucher = Model.GetItemTransactionVoucher(refId);

            View.RefId = voucher.RefId;
            View.RefTypeId = voucher.RefTypeId;
            View.RefNo = voucher.RefNo;
            View.RefDate = voucher.RefDate;
            View.AccountingObjectType = voucher.AccountingObjectType;
            View.CustomerId = voucher.CustomerId == 0 ? null : voucher.CustomerId;
            View.VendorId = voucher.VendorId == 0 ? null : voucher.VendorId;
   
            View.Trader = voucher.Trader;
            View.UnitId = voucher.UnitId;
            View.StockId = voucher.StockId;
            View.TotalAmountOc = voucher.TotalAmountOc;
            View.TaxCode = voucher.TaxCode;
            View.TotalAmountTax = voucher.TotalAmountTax;
            View.JournalMemo = voucher.JournalMemo;
            View.DocumentInclude = voucher.DocumentInclude;
            View.ItemTransactionDetails = voucher.ItemTransactionDetails;
        }

        /// <summary>
        /// Deletes the specified reference identifier.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <returns></returns>
        public string Delete(long refId)
        {
            return Model.DeleteItemTransactionVoucher(refId);
        }

        /// <summary>
        /// Gets the payment voucher by identifier.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <returns></returns>
        public ItemTransactionModel GetItemTransactionById(long refId)
        {
            return Model.GetItemTransactionVoucher(refId);
        }
    }
}
